﻿using RegisterationCommon.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Collections.Specialized;


namespace RegisterationCommon.Preschools
{
    /// <summary>
    /// 幼儿园信息实体类
    /// </summary>
   public class Preschools
    {
       
        //主键，自动编号
        private int _ID = 0;
        //学校名称
        private string _Name = "";

        //用户名
        private string _AccountName = "";
        //密码
        private string _AccountPswd = "";

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
  

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
    

        public string AccountName
        {
            get { return _AccountName; }
            set { _AccountName = value; }
        }
      
        public string AccountPswd
        {
            get { return _AccountPswd; }
            set { _AccountPswd = value; }
        }

        //获取当前账号实例
        private static Preschools _Current;
        /// <summary>
        /// 获取当前账号实例
        /// </summary>
        public static Preschools Current
        {
            get
            {
                if (_Current == null)
                {
                    Preschools._Current = new Preschools();
                }
                return Preschools._Current;
            }
        }


        #region 分页获取学校信息
        /// <summary>
        /// 分页获取学校信息
        /// </summary>
        /// <param name="RP">数据集</param>
        /// <returns>返回JSON格式的字符串</returns>
        public string GetSchoolInfoList(RParams RP)
        {
            //查询当前页
            DataTable dt = DBAction.ExecSP("sp_select_page", RP).Tables[0];
            //查询条件的总条数
            int totalCount = DBAction.GetDTFromSQL("select * from " + ETableName.Preschools + " where " + RP.GetSqlParameter("Where").Value).Rows.Count;
            //返回Json
            return DBAction.DataTableToJson(dt, true, totalCount);
        }
        #endregion

        
        /// <summary>
        /// 不分页获取学校信息
        /// </summary>
        /// <returns>返回获取的DataTable</returns>
        public DataTable GetSchoolInfoList()
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from " + ETableName.Preschools + " ");
            return dt;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="accountID">账号ID</param>
        /// <param name="accountID">MD5加密后的账号密码</param>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfAccountPwd(int accountID, string AccountPswd)
        {
            bool isSucceed = false;

            //非法字符验证
            if (!BaseLibrary.Verification(AccountPswd, false, EVerification.SafeString))
            {
                isSucceed = false;
            }

            RParams RP = new RParams();
            RP.Add("ID", ID, SqlDbType.Int);
            RP.Add("AccountPswd", AccountPswd, SqlDbType.VarChar);

            string colFields = "AccountPswd";
            string whereFields = "ID=@ID";

            //更新学生密码
            if (DBAction.Update(ETableName.Preschools, colFields, whereFields, RP))
            {
                Cookies.Set("AccountPswd", AccountPswd);
                isSucceed = true;
            }
            else
            {
                isSucceed = false;
            }
            return isSucceed;
        }

        /// <summary>
        /// 修改当前登录用户密码
        /// </summary>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfAccountPwd()
        {
           // return MdfAccountPwd(Convert.ToInt32(CookiesInfo.GetValue("Robo", "ID").ToString()), this._AccountPswd);
            return MdfAccountPwd(Convert.ToInt32(Cookies.Get("ID").ToString()), this._AccountPswd);
        }

        public bool UpdateAccountPwd(int ID, string AccountPswd)
        {
            RParams rp = new RParams();
            rp.Add("ID", ID, SqlDbType.Int);
            rp.Add("AccountPswd", AccountPswd, SqlDbType.VarChar);
            return DBAction.Update(ETableName.Preschools, "AccountPswd", "ID=@ID", rp);
        }

        /// <summary>
        /// 根据学校编号查询学校信息
        /// </summary>
        /// <param name="schID">学校编号</param>
        /// <returns>查询成功返回true,否则返回false</returns>
        public bool GetPreschoolsByID(int ID)
        {
            DataTable dt = DBAction.GetDTFromSQL("Select * From " + ETableName.Preschools + " where id=" + ID);
            if (dt.Rows.Count < 1)
            {
                return false;
            }
            this._ID = ID;
            this._Name = Convert.ToString(dt.Rows[0]["Name"]);
            this._AccountName = Convert.ToString(dt.Rows[0]["AccountName"]);
            this._AccountPswd = Convert.ToString(dt.Rows[0]["AccountPswd"]);
            return true;
        }

       /// <summary>
       /// 判断登录
       /// </summary>
       /// <param name="schName"></param>
       /// <param name="schPwd"></param>
       /// <returns></returns>
        public bool Login(string AccountName, string AccountPswd)
        {
            //非法字符验证
            if (!BaseLibrary.Verification(AccountName, false, EVerification.SafeString) || !BaseLibrary.Verification(AccountPswd, false, EVerification.SafeString))
            {
                return false;
            }
            //查询语句
            string sql = "select * from " + ETableName.Preschools + " where AccountName='" + AccountName + "' and AccountPswd='" + AccountPswd + "'";
            DataTable dt = DBAction.GetDTFromSQL(sql);
            if (dt.Rows.Count < 1)
            {
                return false;
            }
            //保存到变量里
            this.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
            this._Name = Convert.ToString(dt.Rows[0]["Name"]);
            this._AccountName = Convert.ToString(dt.Rows[0]["AccountName"]);
            this._AccountPswd = Convert.ToString(dt.Rows[0]["AccountPswd"]);


            Cookies.Set("Name", this._Name);
            Cookies.Set("AccountName", this._AccountName);
            Cookies.Set("AccountPswd", this._AccountPswd);
            Cookies.Set("ID", Convert.ToString(this._ID).ToString());     
            return true;
        }


        /// <summary>
        /// 判断账号是否登录状态，并且账号信息是否安全
        /// </summary>
        /// <returns></returns>
        public bool IsLogin()
        {
            bool isLogin = false;
            if (Cookies.Exists())
            {
                string sql = "select * from " + ETableName.Preschools + " where AccountName='" + Cookies.Get("AccountName") + "' and AccountPswd='" + Cookies.Get("AccountPswd") + "'";
                DataTable dt = DBAction.GetDTFromSQL(sql);
                if (dt.Rows.Count < 1)
                {
                    isLogin = false;
                    return isLogin;
                }
                isLogin = true;
            }

            return isLogin;
        }


    }
}
